﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DotNet.Helper
{
    /// <summary>
    /// 其他帮助类
    /// </summary>
    public class OtherHelper
    {
        /// <summary>
        /// 身份证最后一位数组
        /// </summary>
        private static readonly string[] ValCodeArr = {"1", "0", "x", "9", "8", "7", "6", "5", "4", "3", "2"};

        /// <summary>
        /// 身份证最后一位验证码
        /// </summary>
        private static readonly int[] Wi = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};

        /// <summary>
        /// 身份证省份数组
        /// </summary>
        private static readonly string[] Provinces =
        {
            "11", "12", "13", "14", "15", "21", "22", "23", "31", "32", "33", "34", "35", "36", "37", "41", "42", "43",
            "44", "45", "46", "50", "51", "52", "53", "54", "61", "62", "63", "64", "65", "71", "81", "82"
        };

        /// <summary>
        /// 生成身份证号码
        /// </summary>
        /// <returns></returns>
        public static string CreateIdCard()
        {
            Random random = new Random();
            StringBuilder sb = new StringBuilder(18);
            // 随机生成省、自治区、直辖市代码 1-2
            sb.Append(Provinces[random.Next(0, Provinces.Length)]);
            // 随机生成地级市、盟、自治州代码 3-4
            sb.Append(random.Next(1, 19).ToString().PadLeft(2, '0'));
            // 随机生成县、县级市、区代码 5-6
            sb.Append(random.Next(1, 29).ToString().PadLeft(2, '0'));
            // 随机生成出生年月 7-14
            sb.Append(DateTime.Now.AddYears(-random.Next(20, 50)).ToString("yyyyMMdd"));
            // 随机生成顺序号 15-17(随机性别)
            sb.Append(random.Next(0, 1000).ToString());
            // 随机生成校验码 18
            int totalmulAiWi = 0;
            string id = sb.ToString();
            for (int i = 0; i < 17; i++)
            {
                totalmulAiWi = totalmulAiWi + Convert.ToInt32(id[i].ToString()) * Wi[i];
            }
            int modValue = totalmulAiWi % 11;
            sb.Append(ValCodeArr[modValue]);

            return sb.ToString();
        }


    }
}
